package com.ruoyi.mongo.word.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mongo.service.MongoCommonService;
import com.ruoyi.mongo.word.entity.WordCheckNewRecord;
import com.ruoyi.mongo.word.service.WordCheckRecordInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Slf4j
@Service
public class WordCheckRecordService implements WordCheckRecordInterface {
    @Autowired
    private MongoCommonService mongoCommonService;

    @Override
    public int insertRecordList(List<WordCheckNewRecord> recordList, String collectionName) {
        if (recordList == null || recordList.isEmpty()) {
            return 0;
        }
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = collectionName + "_" + userId;
        String practiceId = String.valueOf(Math.random()).substring(2);
        HashSet<String> hashSet = new HashSet<>();
        recordList.forEach(record -> {
            record.set_id(null);
            record.setPracticeId(practiceId);
            if (record.getName() != null) {
                if (record.getSelected() != null && !record.getSelected().isEmpty()) {
                    record.setResult(record.getChinese().equals(record.getSelected()));
                }
            } else {
                //存在没有获取到数据的情况
                record.setResult(true);
            }
            WordCheckNewRecord checkNewRecord = mongoCommonService.insertOne(record, collection);
            String recordId = checkNewRecord.get_id();
            hashSet.add(recordId);
            log.info("集合:{}-时间:{}-插入了一条记录,主键:{}", collection, DateUtils.getLocalTime(), recordId);
        });

        return hashSet.size();
    }

    public List<WordCheckNewRecord> getRecordList(List<WordCheckNewRecord> recordList,String book,String unit){
        recordList.forEach(record -> {
            record.set_id(null);
            if (record.getSelected() != null && !record.getSelected().isEmpty()) {
                record.setResult(record.getChinese().equals(record.getSelected()));
            }
            record.setBook(book);
            record.setUnit(unit);
        });
        return recordList;
    }

    @Override
    public int insertRecordList(JSONObject jsonObject, String collectionName) {
        List<WordCheckNewRecord> recordList = jsonObject.getList("data",WordCheckNewRecord.class);
        String book = jsonObject.getObject("book", String.class);
        String unit = jsonObject.getObject("unit", String.class);
        List<WordCheckNewRecord> list = getRecordList(recordList, book, unit);
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String collection = collectionName + "_" + userId;
        mongoCommonService.insertList(list, collection);
        return recordList.size();
    }
}
